Component({
  data: {
    globalMsgVisible: false,
    globalMsgTitle: '',
    globalMsgContent: '',
    globalMsgHideTimer: null,
    globalMsgType: '',
    globalMsgCanClick: true,
    globalMsgClickToPage: '',
  },
  methods: {
    /**
     * 显示消息
     * @param {*} globalMsgTitle 
     * @param {*} globalMsgContent 
     * @param {*} globalMsgType 
     * @param {*} globalMsgCanClick 
     */
    showMessage(globalMsgTitle, globalMsgContent, globalMsgType, globalMsgCanClick, globalMsgClickToPage) {
      if (this.data.globalMsgHideTimer) clearTimeout(this.data.globalMsgHideTimer);
      // 震动提醒，优先长震动，兼容所有版本
      if (wx.vibrateLong) {
        wx.vibrateLong();
      } else if (wx.vibrateShort) {
        wx.vibrateShort();
      }
      this.setData({ globalMsgVisible: true, globalMsgTitle, globalMsgContent, globalMsgType, globalMsgCanClick , globalMsgClickToPage});
      this.data.globalMsgHideTimer = setTimeout(() => {
        this.setData({ globalMsgVisible: false });
      }, 3000);
    },
    /**
     * 消息点击后
     */
    onGlobalMsgTap() {
      if (this.data.globalMsgCanClick){
        if (this.data.globalMsgClickToPage){
          // 指定点击跳转的页面
          wx.navigateTo({ url: this.data.globalMsgClickToPage });
          // 跳转到tabBar就使用下面的switchTab方式
          // wx.switchTab({ url: '/pages/profile/profile' });
          this.setData({ globalMsgVisible: false });

        } else {
          wx.navigateTo({ url: '/pages/message-manage/message-manage' });
          // 跳转到tabBar就使用下面的switchTab方式
          // wx.switchTab({ url: '/pages/profile/profile' });
          this.setData({ globalMsgVisible: false });
        }
      } else {
        console.log('消息不可以点击')
      }
      
    }
  }
});
